package com.jx.gocomtask.webservice.service.impl;

import java.net.URI;
import java.net.URISyntaxException;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.jx.gocomtask.webservice.service.SystemSuperApp;

@Service
public class GxinMenu {
	private static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(GxinMenu.class);
	@Resource(name="menuTodo")
	String menuTodo;
	@Resource(name="menuView")
	String menuView;
	@Resource(name="menuList")
	String menuList;
	
	@Resource(name="apiurl")
	String gocomeApiUrl;
	@Autowired
	RestTemplate restTemplate;
	@Autowired
	SystemSuperApp systemSuperApp;
	@Resource(name="defaultAppId")
	String defaultAppId;
	
	public void tryMenu(boolean reset) throws URISyntaxException{
		//String rt = restTemplate.postForObject(gocomeApiUrl + "menu/get?"+systemSuperApp.appid_token(defaultAppId),userBatch,String.class);
		URI uri=new URI(gocomeApiUrl + "menu/get?"+systemSuperApp.appid_token(defaultAppId));
		String rt=restTemplate.getForObject(uri, String.class);
		if(!rt.contains("即信待办")){
			setupMenu();
		}
		if(reset){
			setupMenu();
		}
	}
	
	private void setupMenu() throws URISyntaxException{
		StringBuffer sb=new StringBuffer();
		sb.append("{");
		sb.append("\"button\": [");
		sb.append("{");
		sb.append("\"type\": \"view\",");
		sb.append("\"name\": \"即信待办\",");
		sb.append("\"url\": \""+menuTodo+"\"");
		sb.append("},");
		sb.append("{");
		sb.append("\"type\": \"view\",");
		sb.append("\"name\": \"即信看板\",");
		sb.append("\"url\": \""+menuView+"\"");
		sb.append("},");
		sb.append("{");
		sb.append("\"type\": \"view\",");
		sb.append("\"name\": \"即信一览\",");
		sb.append("\"url\": \""+menuList+"\"");
		sb.append("}");
		sb.append("]");
		sb.append("}");
		restTemplate.postForObject(gocomeApiUrl + "menu/create?"+systemSuperApp.appid_token(defaultAppId),sb.toString(),String.class);
	}
}
